/**
 * 全排列问题
 * result = []
    def backtrack(路径, 选择列表):
        if 满足结束条件:
            result.add(路径)
            return

        for 选择 in 选择列表:
            做选择
            backtrack(路径, 选择列表)
            撤销选择    

 * https://labuladong.gitbook.io/algo/di-ling-zhang-bi-du-xi-lie/hui-su-suan-fa-xiang-jie-xiu-ding-ban
 */
function main(nums) {
    var res = [];
    var track = [];
    backtrack(nums, track);
    
    function backtrack(nums, track) {
        if (nums.length === track.length) {
            res.push([...track]);
            return;
        }
        for (let i = 0; i < nums.length; i++) {
            const item = nums[i]; // 1
            if (track.includes(item)) {
                continue;
            }
            track.push(item); //路径添加选择
            backtrack(nums, track);
    
            track.pop(); //撤销选择
        }
    }

    return res;
}

var nums = [1, 2, 3];
var res = main(nums);
console.log(res);